package com.yangye.cloud.demo.order.controller;

import com.yangye.cloud.demo.order.domain.Order;
import com.yangye.cloud.demo.order.mapper.OrderMapper;
import com.yangye.cloud.demo.order.service.OrderService;
import com.yangye.cloud.demo.order.service.TestComponent;
import com.zaxxer.hikari.HikariConfigMXBean;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.List;

/**
 * @author yangye
 * @date 2020/11/30
 */
@RestController
@RequestMapping("/test")
public class TestController {
	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private ApplicationContext applicationContext;

	@Autowired
	private TestComponent testComponent;

	@GetMapping("/order")
	public List<Order> list() {
		return orderMapper.selectAll();
	}

	@GetMapping("/set")
	public void set(String name) {
		testComponent.setName(name);
	}

	@GetMapping("/get")
	public String get() {
		return testComponent.getName();
	}

	@GetMapping("/change")
	public void changeDatasourcePassword(String password) {
		HikariDataSource dataSource = (HikariDataSource)applicationContext.getBean(DataSource.class);
		HikariConfigMXBean hikariConfigMXBean = dataSource.getHikariConfigMXBean();
		hikariConfigMXBean.setPassword(password);
	}
}
